package com.irdstudio.efp.flow.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.core.util.UUIDUtil;
import com.irdstudio.efp.flow.service.chart.EventChartParser;
import com.irdstudio.efp.flow.service.chart.EventChartParserInfo;
import com.irdstudio.efp.flow.service.dao.BizEventChartInfoDao;
import com.irdstudio.efp.flow.service.dao.BizFlowNodeDao;
import com.irdstudio.efp.flow.service.dao.BizFlowProcessDao;
import com.irdstudio.efp.flow.service.dao.BizFlowRouteDao;
import com.irdstudio.efp.flow.service.domain.BizEventChartInfo;
import com.irdstudio.efp.flow.service.domain.BizFlowNode;
import com.irdstudio.efp.flow.service.domain.BizFlowProcess;
import com.irdstudio.efp.flow.service.domain.BizFlowRoute;
import com.irdstudio.efp.flow.service.facade.BizEventChartInfoService;
import com.irdstudio.efp.flow.service.vo.BizEventChartInfoVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务流转事件图形信息				<p>
 * @author fuzm
 * @date 2018-06-01
 */
@Service("bizEventChartInfoService")
public class BizEventChartInfoServiceImpl implements BizEventChartInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizEventChartInfoServiceImpl.class);

	@Autowired
	private BizEventChartInfoDao bizEventChartInfoDao;
	
	@Autowired
	private BizFlowNodeDao bizFlowNodeDao;
	
	@Autowired
	private BizFlowProcessDao bizFlowProcessDao;
	
	@Autowired
	private BizFlowRouteDao bizFlowRouteDao;
	
	/**
	 * 新增操作
	 */
	public int insertBizEventChartInfo(BizEventChartInfoVO inBizEventChartInfoVo) {
		logger.debug("当前新增数据为:"+ inBizEventChartInfoVo.toString());
		int num = 0;
		try {
			BizEventChartInfo bizEventChartInfo = new BizEventChartInfo();
			beanCopy(inBizEventChartInfoVo, bizEventChartInfo);
			num = bizEventChartInfoDao.insertBizEventChartInfo(bizEventChartInfo);
			
			
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizEventChartInfoVO inBizEventChartInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBizEventChartInfoVo);
		int num = 0;
		try {
			BizEventChartInfo bizEventChartInfo = new BizEventChartInfo();
			beanCopy(inBizEventChartInfoVo, bizEventChartInfo);
			num = bizEventChartInfoDao.deleteByPk(bizEventChartInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventChartInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizEventChartInfoVO inBizEventChartInfoVo) {
		logger.debug("当前修改数据为:"+ inBizEventChartInfoVo.toString());
		int num = 0;
		try {
			BizEventChartInfo bizEventChartInfo = new BizEventChartInfo();
			beanCopy(inBizEventChartInfoVo, bizEventChartInfo);
			num = bizEventChartInfoDao.updateByPk(bizEventChartInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventChartInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizEventChartInfoVO queryByPk(BizEventChartInfoVO inBizEventChartInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizEventChartInfoVo);
		try {
			BizEventChartInfo queryBizEventChartInfo = new BizEventChartInfo();
			beanCopy(inBizEventChartInfoVo, queryBizEventChartInfo);
			BizEventChartInfo queryRslBizEventChartInfo = bizEventChartInfoDao.queryByPk(queryBizEventChartInfo);
			if (Objects.nonNull(queryRslBizEventChartInfo)) {
				BizEventChartInfoVO outBizEventChartInfoVo = beanCopy(queryRslBizEventChartInfo, new BizEventChartInfoVO());
				logger.debug("当前查询结果为:"+ outBizEventChartInfoVo.toString());
				return outBizEventChartInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	
	/**
	 * 保存图形数据
	 */
	public int saveBizEventChartInfo(BizEventChartInfoVO inBizEventChartInfoVo) {
		int num = 0;
		try {
			BizEventChartInfo bizEventChartInfo = new BizEventChartInfo();
			beanCopy(inBizEventChartInfoVo, bizEventChartInfo);
			//查询数据库是否存在
			BizEventChartInfo oldChartInfo = bizEventChartInfoDao.queryByPk(bizEventChartInfo);
			if(oldChartInfo != null && StringUtil.isNotEmpty(oldChartInfo.getBizFlowId())) {
				//存在则更新数据
				bizEventChartInfo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
				bizEventChartInfo.setLastUpdateUser(bizEventChartInfo.getLastUpdateUser());
				num = bizEventChartInfoDao.updateByPk(bizEventChartInfo);
				logger.debug("更新事件流图形信息");
			} else {
				//不存在则创建数据
				bizEventChartInfo.setCreateTime(TimeUtil.getCurrentDateTime());
				bizEventChartInfo.setCreateUser(bizEventChartInfo.getLastUpdateUser());
				num = bizEventChartInfoDao.insertBizEventChartInfo(bizEventChartInfo);
				logger.debug("创建事件流图形信息");
			}
			
			//清除事件节点信息
			BizFlowProcess deleteProcessParam = new BizFlowProcess();
			deleteProcessParam.setBizFlowId(inBizEventChartInfoVo.getBizFlowId());
			deleteProcessParam.setBizEventId(inBizEventChartInfoVo.getBizEventId());
			bizFlowProcessDao.deleteByFlowIdAndEventId(deleteProcessParam);
			
			//清除事件路由信息
			BizFlowRoute deleteRouteParam = new BizFlowRoute();
			deleteRouteParam.setBizFlowId(inBizEventChartInfoVo.getBizFlowId());
			deleteRouteParam.setBizEventId(inBizEventChartInfoVo.getBizEventId());
			bizFlowRouteDao.deleteByFlowIdAndEventId(deleteRouteParam);
			
			//存储路由信息
			if(StringUtil.isNotEmpty(bizEventChartInfo.getEventChartJson())) {
				EventChartParserInfo chartParserInfo = EventChartParser.findRouterInfo(bizEventChartInfo);
				if(chartParserInfo != null) {
					//处理节点信息
					List<BizFlowProcess> processList = chartParserInfo.getProcessList();
					if(processList != null) {
						for(int i=0; i<processList.size(); i++) {
							BizFlowProcess process = processList.get(i);
							
							//查询节点信息
							BizFlowNode param = new BizFlowNode();
							param.setBizNodeId(process.getBizNodeId());
							BizFlowNode flowNode = bizFlowNodeDao.queryByPk(param);
							
							//设置节点信息
							process.setBizFlowId(bizEventChartInfo.getBizFlowId());
							process.setBizEventId(bizEventChartInfo.getBizEventId());
							process.setBizStageId(flowNode.getBizStageId());
							process.setBizStageName(flowNode.getBizStageName());
							process.setBizNodeType(flowNode.getBizNodeType());
							
							//保存
							bizFlowProcessDao.insertBizFlowProcess(process);
						}
					}
					
					//处理路由信息
					List<BizFlowRoute> routeList = chartParserInfo.getRouteList();
					if(routeList != null && routeList.size() > 0) {
						for(int i=0; i<routeList.size(); i++) {
							BizFlowRoute route = routeList.get(i);
							route.setBizRouteId(UUIDUtil.getUUID());
							bizFlowRouteDao.insertBizFlowRoute(route);
						}
					}
				} 
			}
			
		} catch (Exception e) {
			logger.error("保存数据发生异常!", e);
			num = -1;
		}
		
		return num;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventChartInfoVO> queryAllOwner(BizEventChartInfoVO bizEventChartInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizEventChartInfoVO> list = null;
		try {
			List<BizEventChartInfo> bizEventChartInfos = bizEventChartInfoDao.queryAllOwnerByPage(bizEventChartInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizEventChartInfos.size());
			pageSet(bizEventChartInfos, bizEventChartInfoVo);
			list = (List<BizEventChartInfoVO>) beansCopy(bizEventChartInfos, BizEventChartInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventChartInfoVO> queryAllCurrOrg(BizEventChartInfoVO bizEventChartInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizEventChartInfo> bizEventChartInfos = bizEventChartInfoDao.queryAllCurrOrgByPage(bizEventChartInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizEventChartInfos.size());
		List<BizEventChartInfoVO> list = null;
		try {
			pageSet(bizEventChartInfos, bizEventChartInfoVo);
			list = (List<BizEventChartInfoVO>) beansCopy(bizEventChartInfos, BizEventChartInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventChartInfoVO> queryAllCurrDownOrg(BizEventChartInfoVO bizEventChartInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizEventChartInfo> bizEventChartInfos = bizEventChartInfoDao.queryAllCurrDownOrgByPage(bizEventChartInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizEventChartInfos.size());
		List<BizEventChartInfoVO> list = null;
		try {
			pageSet(bizEventChartInfos, bizEventChartInfoVo);
			list = (List<BizEventChartInfoVO>) beansCopy(bizEventChartInfos, BizEventChartInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
